package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.uitl.DBHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository strategyCommentRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public StrategyComment get(String id) {
        return strategyCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> lists() {
        return strategyCommentRepository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        //创建查询对象
        Query query=new Query();
        //添加条件所有字符串都要判空的
        if (StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        //分页查询
        Pageable pageRequest=PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.DESC,"createTime");
        return DBHelper.query(mongoTemplate,StrategyComment.class,query,pageRequest);
    }
    //添加评论
    @Override
    public void addComment(StrategyComment strategyComment, UserInfo userInfo) {
        BeanUtils.copyProperties(userInfo,strategyComment);
        strategyComment.setUserId(userInfo.getId());
        strategyComment.setCreateTime(new Date());
        this.save(strategyComment);
    }
    //点赞操作
    @Override
    public void commentThumb(String cid, String uid) {
        //拿到当前评论
        StrategyComment strategyComment = this.get(cid);
        //判断
        List<String> thumbuplist = strategyComment.getThumbuplist();
        if (thumbuplist.contains(uid)){
            //取消功能
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
            thumbuplist.remove(uid);
        }else {
            //点赞功能
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
            thumbuplist.add(uid);
        }
        this.update(strategyComment);
    }
}
